package  
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextFormat;
	import ginger.geom.RectangleShape;
	import ginger.ui.simple.SimpleBrightnessButton;
	import ginger.ui.simple.SimpleLabel;
	import ginger.utils.DisplayUtil;
	import interactive.turnAnimation.TurnRenderer;
	import UILib.DetailBtnBmpdata;
	import caurina.transitions.Tweener;
	
	/**
	 * 宣染器
	 * @author Ning
	 */
	public class ItemRenderer extends TurnRenderer 
	{
		private var _box:Sprite;
		private var _mask:RectangleShape;
		private var _loader:Loader;
		private var _url:String;
		private var _id:String;
		private var _label:SimpleLabel;
		private var _titleBg:RectangleShape;
		private var _bmpBox:Sprite;
		private var _isLoad:Boolean = false;
		private var _labelBox:Sprite;
		private var _deltailBtn:SimpleBrightnessButton;

		public function ItemRenderer() 
		{
			super()
			setLayoutSize(404, 259);
			
			graphics.beginFill(0xffffff);
			graphics.drawRect(0, 0, 404, 259);
			graphics.endFill();
			
			_box = new Sprite();
			_mask = new RectangleShape(400, 255);
			_box.x=_mask.x = 2;
			_box.y = _mask.y = 2;
			_box.mask = _mask;
			
			_bmpBox = new Sprite();
			_box.addChild(_bmpBox);
			
			_titleBg = new RectangleShape(404, 33, 0, .3);
			_titleBg.y = _mask.height - _titleBg.height;
			_box.addChild(_titleBg);
			
			_labelBox = new Sprite();
			_box.addChild(_labelBox);
			
			_label = new SimpleLabel();
			_label.defaultTextFormat=new TextFormat('宋体',14,0xffffff,true)
			_labelBox.x = 10;
			_labelBox.y = 230;
			
			var btnBmp:Bitmap = new Bitmap(new DetailBtnBmpdata);
			btnBmp.smoothing = true;
			_deltailBtn = new SimpleBrightnessButton(btnBmp);
			_deltailBtn.x = 320;
			_deltailBtn.y = 0;
			_labelBox.addChild(_deltailBtn);
			_deltailBtn.addEventListener(MouseEvent.CLICK, onDetailClick);
	        
			addChild(_box);
			addChild(_mask);
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
		}
		public function setData(content:String, id:String, url:String):void
		{
			_label.text = content;
			var labelBmp:Bitmap = DisplayUtil.getBitmapByDisplay(_label);
			labelBmp.smoothing = true;
			_labelBox.addChild(labelBmp);
			
			_url = url;
			_id = id;
		}
		public function load():void
		{
			if (_isLoad)
			    return;
			_isLoad = true;
			if(_url!=null)
			    _loader.load(new URLRequest(_url), new LoaderContext(true));
		}
		private function onComplete(evt:Event):void
		{
			var bmp:Bitmap = evt.target.content as Bitmap;
			bmp.smoothing = true;
			_bmpBox.addChild(bmp);
			bmp.alpha = 0;
			Tweener.addTween(bmp, { time:.5, alpha:1 } );
			
		}
		private function onIOError(evt:IOErrorEvent):void
		{
			trace('图片载入错误'+_url);
		}
		private function onDetailClick(evt:MouseEvent):void
		{
			if (ExternalInterface.available)
			    ExternalInterface.call('getItemId', _id);
		}
	}
}